package facilius.controller.action.nota;

import facilius.model.ConnectionManager;
import facilius.model.ServiceLocator;
import java.io.InputStream;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import org.mentawai.core.BaseAction;
import org.mentawai.core.StreamConsequence;

/**
 *
 * @author Graciano
 */
public class NotaReportAction extends BaseAction {

    @Override
    public String execute() throws Exception {
        String consequence = ERROR;
        try {
            String urlJasper = "/facilius/controller/report/nota/notasProfessor.jasper";
            InputStream is = NotaReportAction.class.getResourceAsStream(urlJasper);

            Connection connection = ConnectionManager.getInstance().getConnection();

            Map parametros = new HashMap();
            parametros.put("turmaId", input.getLong("turmaId"));
            parametros.put("caminhoImagem", "../../css/images/topo - Cópia.jpg");
            parametros.put("descricaoTurma", ServiceLocator.getTurmaService().readById(input.getLong("turmaId")).toString());

            JasperPrint jasperPrint = JasperFillManager.fillReport(is, parametros, connection);

            byte[] relatorio = JasperExportManager.exportReportToPdf(jasperPrint);

            output.setValue(StreamConsequence.STREAM_KEY, relatorio);
            output.setValue(StreamConsequence.CONTENT_LENGTH_KEY, relatorio.length);
            output.setValue(StreamConsequence.FILENAME_KEY, "Desempenho_Geral_Notas");
            output.setValue("cont", java.lang.System.currentTimeMillis());
            consequence = SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return consequence;
    }
}
